package org.sy.lab.多线程基础._4_执行器._2_Future线程控制._3_处理所有结果;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

//invokeAll 接收一个任务列表，等待所有任务完成
public class Main {

	public static void main(String[] args) {
		
		ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
		
		MyCallable myCallable = new MyCallable(5, "任务1");
		MyCallable myCallable2 = new MyCallable(2, "任务2");
		
		List<MyCallable> list =new ArrayList<MyCallable>();
		list.add(myCallable);
		list.add(myCallable2);
		
		List<Future<String>> result = new ArrayList<Future<String>>();
		try {
			result = executor.invokeAll(list);
			for (Iterator<Future<String>> iterator = result.iterator(); iterator.hasNext();) {
				Future<String> future = (Future<String>) iterator.next();
				System.out.println(future.get());
			}
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
		executor.shutdown();
	}
}
